/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package com.sunyard.client2.util;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:字符串工具类
 * 
 * @author <a href="mailto:jh200326630615@163.com">Watermud</a>
 * @version 1.0
 * @date 2010-3-25
 */

public class StringUtil {

	private static String NEWLINE = System.getProperty("line.separator");

	/**
	 * 首字母大写
	 * 
	 * @param src
	 * @return
	 */
	public static String capitalize(String src) {
		if (src == null || src.length() == 0) {
			return "";
		}
		return src.substring(0, 1).toUpperCase() + src.substring(1);
	}

	public static String lpad(String str, int len, String padding) {
		if (str == null) {
			return str;
		}
		if (str.length() >= len) {
			return str;
		}
		if (padding == null) {
			padding = " ";
		}
		int needLen = len - str.length();
		int paddingLength = padding.length();
		StringBuilder result = new StringBuilder();
		int m = needLen / paddingLength;
		int n = needLen % paddingLength;
		for (int i = 0; i < m; i++) {
			result.append(padding);
		}
		if (n > 0) {
			result.append(padding.subSequence(0, n));
		}
		result.append(str);
		return result.toString();

	}

	public static String rpad(String str, int len, String padding) {
		if (str == null) {
			return str;
		}
		if (str.length() >= len) {
			return str;
		}
		if (padding == null) {
			padding = " ";
		}
		int needLen = len - str.length();
		int paddingLength = padding.length();
		StringBuilder result = new StringBuilder(str);
		int m = needLen / paddingLength;
		int n = needLen % paddingLength;
		for (int i = 0; i < m; i++) {
			result.append(padding);
		}
		if (n > 0) {
			result.append(padding.subSequence(0, n));
		}
		return result.toString();

	}

	/**
	 * 首字母小写
	 * 
	 * @param src
	 * @return
	 */
	public static String unCapitalize(String src) {
		if (src == null || src.length() == 0) {
			return "";
		}
		return src.substring(0, 1).toLowerCase() + src.substring(1);
	}

	/**
	 * 生成当前日期的字符串
	 * 
	 * @param split
	 *            年月日之间的分隔符
	 * @return
	 */
	public static String getCurrentDateStr(String split) {
		if (split == null) {
			split = "";
		}
		Calendar cal = Calendar.getInstance();
		int y = cal.get(Calendar.YEAR);
		int m = cal.get(Calendar.MONTH) + 1;
		int d = cal.get(Calendar.DAY_OF_MONTH);
		StringBuilder result = new StringBuilder();
		result.append(y);
		result.append(split);
		result.append(m > 9 ? m : "0" + m);
		result.append(split);
		result.append(d > 9 ? d : "0" + d);
		return result.toString();
	}

	/**
	 * 
	 * @param text
	 * @return 如果传入null，则返回null ，其他返回trim结果
	 */
	public static String trim(String text) {
		if (text == null) {
			return null;
		}
		return text.trim();
	}

	/**
	 * 
	 * @param text
	 * @return 如果传入null，则返回null ，其他返回trim结果
	 */
	public static String[] trim(String[] texts) {
		if (texts == null) {
			return null;
		}
		String[] results = new String[texts.length];
		for (int i = 0; i < texts.length; i++) {
			results[i] = trim(texts[i]);
		}
		return results;
	}

	/**
	 * 根据驼峰规则返回字符串
	 * 
	 * @param text
	 * @return 如果传入null，则返回null ，其他返回结果
	 */
	public static String getJavaStyleName(String name) {
		if (name == null) {
			return null;
		}
		String[] slips = name.toLowerCase().split("_");
		if (slips.length == 1) {
			return unCapitalize(slips[0]);
		}
		StringBuilder string = new StringBuilder(unCapitalize(slips[0]));
		for (int i = 1; i < slips.length; i++) {
			if (Character.isLetter(slips[i].charAt(0))) {
				string.append(capitalize(slips[i]));
			} else {
				string.append("_").append(slips[i]);
			}
		}
		return string.toString();
	}

	/**
	 * 根据驼峰规则返回字符串
	 * 
	 * @param text
	 * @return 如果传入null，则返回null ，其他返回结果
	 */
	public static String getDBName(String name) {
		if (StringUtil.isNull(name)) {
			return null;
		}
		byte[] bt = name.getBytes();
		byte[] result = new byte[bt.length * 2];
		int count = 0;
		for (int i = 0; i < bt.length; i++) {
			if (bt[i] >= 65 && bt[i] <= 90) {
				result[count++] = '_';
				result[count++] = (byte) (bt[i] + 32);
			} else {
				result[count++] = bt[i];
			}
		}
		return new String(result, 0, count);
	}

	/**
	 * 判断输入的字符串为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNull(String str) {
		return str == null || str.length() == 0 || str.trim().length() == 0;
	}

	/**
	 * 判断输入的字符串不为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotNull(String str) {
		return str != null && str.trim().length() > 0;
	}

	/**
	 * 返回格式化的金额数字
	 * 
	 * @param number
	 * @return
	 */
	public static String numformat(double number) {
		String numstr;
		DecimalFormat digitformat = new DecimalFormat("###############0.00");
		numstr = digitformat.format(number);
		return numstr;
	}

	/**
	 * 返回格式化的金额数字
	 * 
	 * @param tmp
	 * @return
	 */
	public static String numformat(String tmp) {
		String numstr;
		double tmp1 = Double.parseDouble(tmp);
		DecimalFormat digitformat = new DecimalFormat("###############0.00");
		numstr = digitformat.format(tmp1);
		return numstr;
	}

	@Deprecated
	public static boolean parseBoolean(String tmp) {
		if (isNull(tmp)) {
			return false;
		}
		tmp = tmp.trim().toUpperCase();
		if (tmp.intern() == "1" || tmp.intern() == "Y" || tmp.intern() == "YES"
				|| tmp.intern() == "T" || tmp.intern() == "TRUE") {
			return true;
		} else {
			return false;
		}
	}

	public static boolean parseBoolean(String tmp, boolean defaultV) {
		if (isNull(tmp)) {
			return defaultV;
		}
		tmp = tmp.trim().toUpperCase();
		if (tmp.intern() == "1" || tmp.intern() == "Y" || tmp.intern() == "YES"
				|| tmp.intern() == "T" || tmp.intern() == "TRUE") {
			return true;
		} else if (tmp.intern() == "0" || tmp.intern() == "N"
				|| tmp.intern() == "NO" || tmp.intern() == "F"
				|| tmp.intern() == "FALSE") {
			return false;
		} else {
			return defaultV;
		}
	}

	public static String getLineSeparator() {
		return NEWLINE;
	}

	public static String[] split(String splitee, String spliter) {
		if (splitee == null) {
			return null;
		} else {
			return splitee.split(spliter);
		}
	}

	public static String removeFileExtend(String str) {
		if (isNull(str))
			return str;
		int i = str.lastIndexOf(".");
		if (i >= 0) {
			return str.substring(0, i);
		}
		return str;
	}

	public static String subString(String src, String bgn, String end) {
		int len = bgn.length();
		int i = src.indexOf(bgn);
		if (i < 0) {
			return src;
		}
		int j = src.indexOf(end, i + 1);
		if (j < 0) {
			return src.substring(i + len);
		}
		return src.substring(i + len, j);
	}

	public static boolean isNum(char c) {
		if (c > 47 && c < 58) {
			return true;
		} else {
			return false;
		}
	}

	public static String removeSpecial(String value) {
		String regEx = "[`~!@#$%^&*()+=|{}':;' ,//[//].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(value);
		return m.replaceAll("").trim();
	}

	public static boolean isLetter(char c) {
		if ((c > 64 && c < 91) || (c > 96 && c < 123)) {
			return true;
		} else {
			return false;
		}
	}

	public static void main(String[] args) {
		System.out.println(StringUtil.lpad("123", 6, "#$"));
	}
}
